package cn.com.chec.en.controller.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.ChannelInfoDao;
import cn.com.chec.en.dao.LanguageInfoDao;
import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.model.domain.ChannelInfo;
import cn.com.chec.en.model.domain.LanguageInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.StringTools;

@Controller
public class JoinUsInfoAdminController {
	protected static final Logger logger = Logger
			.getLogger(JoinUsInfoAdminController.class);

	@Resource
	private LanguageInfoDao languageInfoDao;

	@Resource
	private ChannelInfoDao channelInfoDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private Map<String, Object> environment;

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/admin/joinus/list" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/newsInfo/list/joinus");
		UserInfo userInfoSession = (UserInfo) session
				.getAttribute("systemUserInfo");
		List<String> userRightsSession = (List<String>) session
				.getAttribute("systemUserRigthList");

		int roleId = userInfoSession.getRoleId();
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : userInfoSession
				.getId();
		if ((roleId == 1) && (userId == userInfoSession.getId())) {
			userId = 0;
		}
		String keywords = StringUtils.isNotBlank(request
				.getParameter("keywords")) ? request.getParameter("keywords")
				: "";
		keywords = StringTools.converISO2UTF8(keywords);
		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.parseInt(request
				.getParameter("languageId")) : 1;
		int channelId = StringUtils.isNotBlank(request
				.getParameter("channelId")) ? Integer.parseInt(request
				.getParameter("channelId")) : 5;
		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		QueryNewsObject query = new QueryNewsObject();
		query.setLanguageId(languageId);
		query.setKeywords(keywords);
		query.setStart(pageBean.getStart());
		query.setNum(pageBean.getRowsPerPage());
		query.setUserId(userId);

		List<NewsDTO> newsDTOList = this.newsInfoDao.getNewsListByQueryPChannelId(query,
				channelId);
		pageBean.setMaxRowCount(this.newsInfoDao.getNewsCountByQueryPChannelId(
				query, channelId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		Map<Integer, String> channelMap = this.channelInfoDao.getAllChannelAsMap();
		Map<Integer, String> languageMap = this.languageInfoDao.getLanuageMap();
		List<ChannelInfo> channelInfoList = this.channelInfoDao
				.getSubListByParentIdLanguageId(languageId, 5, 0, 2147483647);
		List<UserInfo> userInfoList = new UserInfoDao().getUserListByRoleId(0, 0,
				2147483647);
		List<LanguageInfo> languageInfoList = this.languageInfoDao.getAll();

		map.put("channelMap", channelMap);
		map.put("languageMap", languageMap);
		map.put("channelInfoList", channelInfoList);
		map.put("userInfoList", userInfoList);
		map.put("languageInfoList", languageInfoList);

		map.put("pageBean", pageBean);
		map.put("newsDTOList", newsDTOList);

		map.put("userInfoSession", userInfoSession);
		map.put("userRightsSession", userRightsSession);
		map.put("channelId", Integer.valueOf(channelId));
		map.put("query", query);
		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}
}